﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace Purdue.Helper
{
    public static class MouseToCommandHelper
    {

        public static ICommand GetMouseDownToCommand(DependencyObject obj)
        {
            if (obj != null)
                return (ICommand)obj.GetValue(MouseDownToCommandProperty);

            return null;
        }

        public static void SetMouseDownToCommand(DependencyObject obj, ICommand value)
        {
            if (obj != null)
                obj.SetValue(MouseDownToCommandProperty, value);
        }
        public static readonly DependencyProperty MouseDownToCommandProperty =
            DependencyProperty.RegisterAttached("MouseDownToCommand", typeof(ICommand), typeof(MouseToCommandHelper), new PropertyMetadata(OnMouseDownToCommandChanged));


        static private void OnMouseDownToCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as FrameworkElement;
            element.MouseDown += element_MouseDown;
        }

        static void element_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var command = GetMouseDownToCommand(sender as DependencyObject);
            if (command != null)
            {
                command.Execute(null);
            }
        }
    }
}
